package com.xxh.market.dao.impl;

import com.xxh.market.dao.BookDao;
import com.xxh.market.dao.BookTypeDao;
import com.xxh.market.entity.Booktype;
import com.xxh.market.utils.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookTypeDaoImpl implements BookTypeDao {
    Connection conn=null;
    PreparedStatement psmt=null;
    ResultSet rs=null;
    @Override
    public List<Booktype> getAllTypes() {
        conn = jdbcUtils.getConnection();
        String sql ="select * from booktype";
        try {
            psmt = conn.prepareStatement(sql);
            rs= psmt.executeQuery();
            ArrayList<Booktype> booktypes = new ArrayList<>();
            while (rs.next()){

                int typeid = rs.getInt(1);
                String typename = rs.getString(2);
                Booktype booktype = new Booktype();
                booktype.setId(typeid);
                booktype.setTypename(typename);

                booktypes.add(booktype);
            }
            return booktypes;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcUtils.close(rs,psmt,conn);
        }
        return null;
    }
}
